#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

static int isrunning = 1;

void exit_process(int signo)
{
    printf("收到信号%d, 正在退出...\r\n", signo);
    if (signo == SIGINT)
    {
        exit(0);
    }
    else if (signo == SIGTERM)
    {
        isrunning = 0;
    }
}

void useResource(void)
{
    printf("使用资源...\r\n");
}

void freeResource(void)
{
    printf("释放资源...\r\n");
}

int main(int argc, char const *argv[])
{
    signal(SIGINT, exit_process); // SIGINT: Ctrl+C
    signal(SIGTERM, exit_process); // SIGTERM: kill
    useResource();
    while (isrunning)
    {
        sleep(1);
        printf("程序执行中...\r\n");
    }
    freeResource();
    return 0;
}

